package com.tros.customer.features;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tros.connection.database.DbConnection;

/**
 * Servlet implementation class ReqCreditCard
 */
@WebServlet("/ReqCreditCard")
public class ReqCreditCard extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReqCreditCard() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
			 
		HttpSession ses = request.getSession();
		System.out.println("++++++++++++"+ses.getAttribute( "currentID" ));
		
		try{
			
			DbConnection con = new DbConnection();			
			ReqCard(con,ses,response);
			
		}catch(Exception e){
			
			System.out.println("Failure in Credit card req process");			
		}		
	}

	private void ReqCard(DbConnection con, HttpSession ses, HttpServletResponse response) {
		// TODO Auto-generated method stub
		
		try{
			
			PreparedStatement state=con.getConnect().prepareStatement("SELECT * FROM CRE_CARDREQ WHERE "+"CUSTOMERID="+ses.getAttribute("currentID"));
			ResultSet result=state.executeQuery();
			
			if(result.next()){
				
				ses.setAttribute("creditcardStatus", "Credit Card Application is already Sent and in Process");
		        response.sendRedirect("resultCredit.jsp");
			}else
			{
				try{
				
					PreparedStatement req=con.getConnect().prepareStatement("INSERT INTO CRE_CARDREQ VALUES(?,?)");
					req.setString(1,String.valueOf(ses.getAttribute("currentID")));
					req.setString(2,"Request");
					req.executeUpdate();
					req.close();
					
					ses.setAttribute("creditcardStatus", "Credit Card Application is sent for Manager Approval");
			        response.sendRedirect("resultCredit.jsp");
					
				}catch(Exception e)
				{
					System.out.println("Failure in updating req");
				}
			}
			
		}catch(Exception e){
			
			System.out.println("Failure in Credit req");
			
		}
	}

}
